<!-- 查看申请信息 -->
<template>
  <el-dialog v-model="dialogVisible" :title="title" width="50%" @close="closeDialog">
    <el-form :model="applyModel" label-width="150px" style="padding-right: 30px">
      <el-form-item label="姓名：" prop="title">
        <el-input v-model="applyModel.name" disabled></el-input>
      </el-form-item>
      <el-form-item label="学生身份证号：" prop="studentCardId">
        <el-input v-model="applyModel.studentCardId" disabled></el-input>
      </el-form-item>
      <el-form-item label="性别：" prop="sex">
        <el-input v-model="applyModel.sex" disabled></el-input>
      </el-form-item>
      <el-form-item label="地址：" prop="domicile">
        <el-input v-model="applyModel.domicile" disabled></el-input>
      </el-form-item>
      <el-form-item label="生日：" prop="birthday">
        <el-input v-model="applyModel.birthday" disabled></el-input>
      </el-form-item>
      <el-form-item label="申请时间：" prop="updateTime">
        <el-input v-model="applyModel.birthday" disabled></el-input>
      </el-form-item>
      <el-form-item label="家长身份证号：" prop="parentCardId">
        <el-input v-model="applyModel.parentCardId" disabled></el-input>
      </el-form-item>
      <el-form-item label="家长手机号：" prop="parentPhone">
        <el-input v-model="applyModel.parentPhone" disabled></el-input>
      </el-form-item>
      <el-form-item label="申请状态：" prop="status">
        <template #default="{ row }">
          <el-tag v-if="applyModel.status === 1" type="success">已审核</el-tag>
          <el-tag v-if="applyModel.status === 2" type="warning">待审核</el-tag>
          <el-tag v-if="applyModel.status === 4" type="warning">解绑待审核</el-tag>
          <el-tag v-if="applyModel.status === 5" type="success">解绑通过</el-tag>
          <el-tag v-if="applyModel.status === 6" type="danger">解绑不通过</el-tag>
        </template>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="flex justify-center items-center">
        <el-button @click="closeDialog">返回</el-button>
        <el-button
          type="primary"
          v-show="applyModel.status == 2 || applyModel.status == 4"
          @click="acceptApply()"
        >
          同意
        </el-button>
        <el-button type="danger" v-show="applyModel.status == 4" @click="disUnbin()">
          不同意
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import {
  accountApplyDetail,
  accountApplyStatus,
  accountUnbindDetail,
  accountUnbindStatus,
} from "@/api/account/index";
import message from "@/composable/ElMessage";

const dialogVisible = ref(false);
const title = ref("");

const applyModel = reactive({
  id: 0,
  name: "",
  parentCardId: "",
  parentPhone: "",
  sex: "",
  domicile: "",
  birthday: "",
  studentCardId: "",
  status: 0,
  updateTime: "",
});

const open = async (t: string, row: any) => {
  dialogVisible.value = true;
  title.value = t;
  if (t == "查看申请信息") {
    const { data } = await accountApplyDetail(row.id);
    Object.assign(applyModel, data);
  }
  if (t == "解绑申请信息") {
    const { data } = await accountUnbindDetail(row.id);
    Object.assign(applyModel, data);
  }
};

const closeDialog = () => {
  dialogVisible.value = false;
};
defineExpose({ open, close });

const emit = defineEmits(["getInfo"]);

const acceptApply = async () => {
  let query = {
    id: applyModel.id,
    status: applyModel.status,
  };

  if (query.status == 2) {
    query.status = 1;
    await accountApplyStatus(query).then((res: any) => {
      // debugger;
      // console.log(data.code);
      if (res.code === "200") {
        message.success("操作成功");
        closeDialog();
        dialogVisible.value = false;
        emit("getInfo");
      }
    });
  }

  if (query.status == 4) {
    query.status = 5;
    await accountUnbindStatus(query).then((res: any) => {
      // debugger;
      // console.log(data.code);
      if (res.code === "200") {
        message.success("操作成功");
        closeDialog();
        dialogVisible.value = false;
        emit("getInfo");
      }
    });
  }
};

const disUnbin = async () => {
  let query = {
    id: applyModel.id,
    status: 6,
  };
  await accountUnbindStatus(query).then((res: any) => {
    // debugger;
    // console.log(data.code);
    if (res.code === "200") {
      message.success("操作成功");
      closeDialog();
      dialogVisible.value = false;
      emit("getInfo");
    }
  });
};
</script>
